<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="消息标题"
        v-model="queryForm.title"
      >
      </el-input>
      <el-select
        clearable
        style="width: 200px"
        class="filter-item"
        v-model="queryForm.status"
        placeholder="请选择发布状态"
      >
        <el-option
          v-for="(val, key) in typeOptions"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
      <!-- <el-button  v-permission="'message:detail'" class="filter-item" @click="doQuery" type="default" icon="el-icon-message" >发布</el-button> -->
      <el-button
        v-permission="'message:add'"
        class="filter-item"
        @click="openCreateDialog"
        type="success"
        icon="el-icon-edit"
        >新增</el-button
      >
    </div>
    <el-table v-loading="loading" :data="records" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="标题">
              <span>{{ scope.row.title }}</span>
            </el-form-item>
            <el-form-item label="内容">
              <span>{{ scope.row.content }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="消息标题" width="250" align="center">
        <template slot-scope="scope">
          <template v-if="scope.row.title">
            {{
              scope.row.title.length > 30
                ? scope.row.title.substring(0, 30) + '...'
                : scope.row.title
            }}
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="content" label="内容" width="300" align="center">
        <template slot-scope="scope">
          <template v-if="scope.row.content">
            {{
              scope.row.content.length > 30
                ? scope.row.content.substring(0, 30) + '...'
                : scope.row.content
            }}
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="targetType"
        label="对象类型"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.MSG_PUSH_TARGET_TYPE[scope.row.targetType] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="发布时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="status"
        label="发布状态"
        width="250"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.PUSH_STATUS[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        width="300"
        align="center"
        label="操作"
        class-name="small-padding"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'message:detail'"
            @click="detailMessage(scope.row.rowId)"
            type="primary"
            size="mini"
            icon="el-icon-check"
            circle
            title="详情"
          ></el-button>
          <el-button
            v-permission="'message:update'"
            @click="editMessageForm(scope.row.rowId)"
            type="success"
            size="mini"
            icon="el-icon-edit"
            circle
            title="编辑"
          ></el-button>
          <el-button
            v-permission="'message:delete'"
            @click="handleDelete(scope.row.rowId)"
            size="mini"
            type="danger"
            icon="el-icon-delete"
            circle
            title="删除"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>
    <!-- 修改新增推送消息弹框 -->
    <el-dialog
      :title="`${editForm.rowId ? '编辑' : '新增'}推送消息`"
      :visible.sync="dialogFormVisible"
      width="30%"
    >
      <el-form
        :model="editForm"
        label-width="90px"
        :rules="editFormRules"
        ref="editForm"
      >
        <el-form-item label="标题:" prop="title">
          <el-input v-model="editForm.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="内容:" prop="content">
          <el-input v-model="editForm.content" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="发布状态:" prop="status">
          <el-select
            v-model="editForm.status"
            placeholder="发布状态"
            style="width: 100%;"
          >
            <el-option
              v-for="(val, key) in Dicts.PUSH_STATUS"
              :key="key"
              :label="val"
              :value="key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目标类型:" prop="targetType">
          <el-select
            v-model="editForm.targetType"
            placeholder="目标类型"
            style="width: 100%;"
          >
            <el-option
              v-for="(val, key) in Dicts.MSG_PUSH_TARGET_TYPE"
              :key="key"
              :label="val"
              :value="key"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveMessage(editForm.rowId)"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 推送消息详情弹框 -->
    <el-dialog title="推送详情" :visible.sync="dialogDetailVisible" width="50%">
      <el-form :model="detailForm" label-width="80px" ref="detailForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题:" prop="title">
              {{ detailForm.title }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="推送方式:" prop="kind">
              {{ detailForm.kind }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="目标类型:" prop="targetType">
              <template slot-scope="scope">
                {{ Dicts.MSG_PUSH_TARGET_TYPE[detailForm.targetType] }}
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布状态:" prop="status">
              <template slot-scope="scope">
                {{ Dicts.PUSH_STATUS[detailForm.status] }}
              </template>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="内容:"
              prop="content"
              style="word-wrap: break-word"
            >
              {{ detailForm.content }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDetailVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryMessage,
  deleteMessage,
  detailMessage,
  addOrEditMessage
} from '@/api/message'

export default {
  name: 'message',
  data() {
    return {
      dialogFormVisible: false,
      dialogDetailVisible: false,
      loading: false,
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        status: undefined,
        title: undefined
      },
      editFormRules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        type: [
          { required: true, message: '请选择目标类型', trigger: 'change' }
        ],
        status: [
          { required: true, message: '请选择发送状态', trigger: 'change' }
        ]
      },
      editForm: {
        rowId: 0,
        targetType: '',
        title: '',
        status: '',
        content: ''
      },
      detailForm: {
        title: undefined,
        content: undefined,
        type: undefined,
        targetType: undefined,
        kind: undefined,
        isDrill: undefined,
        param: undefined,
        publish_time: undefined,
        status: undefined,
        create_by: undefined,
        create_time: undefined,
        modify_by: undefined
      },
      typeOptions: Dicts.PUSH_STATUS,
      records: []
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryMessage(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    handleDelete(rowId) {
      this.$confirm('此操作将永久删除该消息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMessage(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    },
    editMessageForm(rowId) {
      detailMessage(rowId).then(({ data: { code, data, msg } }) => {
        this.openCreateDialog()
        this.editForm = {
          rowId: data.rowId,
          title: data.title,
          content: data.content,
          targetType: data.targetType,
          status: data.status.toString()
        }
      })
    },
    saveMessage(rowId) {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          addOrEditMessage(this.editForm).then(
            ({ data: { data, msg, code } }) => {
              if (code === 0) {
                this.doQuery()
                this.dialogFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        }
      })
    },
    detailMessage(rowId) {
      detailMessage(rowId).then(({ data: { code, data, msg } }) => {
        this.openDetailDialog()
        this.detailForm = {
          title: data.title,
          content: data.content,
          type: data.type,
          targetType: data.targetType,
          kind: data.kind,
          isDrill: data.isDrill,
          param: data.param,
          publishTime: data.publishTime,
          status: data.status.toString(),
          createBy: data.createBy,
          createTime: data.createTime,
          modifyBy: data.modifyBy
        }
      })
    },
    openDetailDialog() {
      this.editForm = {}
      this.dialogDetailVisible = true
      this.$refs.detailForm && this.$refs.detailForm.resetFields()
    },
    openCreateDialog() {
      this.editForm = {}
      this.dialogFormVisible = true
      this.$refs.editForm && this.$refs.editForm.resetFields()
    }
  },
  components: {}
}
</script>

<style scoped></style>
